package cn.edu.stu.syllabus.oa.list;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.adapters.OaAdapter;
import cn.edu.stu.syllabus.base.BaseActivity;
import cn.edu.stu.syllabus.base.BaseFragment;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.base.StoreProxy;
import cn.edu.stu.syllabus.oa.detail.OaDetailActivity;
import cn.edu.stu.syllabus.repository.oa.local.OaInfoEntry;
import cn.edu.stu.syllabus.utils.RecyclerPaddingUtils;
import cn.edu.stu.syllabus.utils.SwipeLayoutOffsetUtils;
import cn.edu.stu.syllabus.widgets.CustomLoadMoreView;
import cn.edu.stu.syllabus.widgets.DividerItemDecoration;
import cn.edu.stu.syllabus.widgets.CustomLayoutManger;

import static cn.edu.stu.syllabus.utils.NullPointerUtils.checkNotNull;

/**
 * Created by liyujie on 2017/3/14.
 */

public class OaFragment extends BaseFragment implements OaContract.View,
        SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener,
        BaseActivity.OnToolbarDoubleClickListener,
        OaActivity.OnClearEnsureListener {


    @BindView(R.id.oaRecyclerView)
    RecyclerView mOaRecyclerView;

    private View mEmptyView;
    private View mErrorView;

    private OaAdapter mOaAdapter;

    private OaContract.Presenter mPresenter;

    CustomLayoutManger mLayoutManger;


    public static OaFragment newInstance() {
        OaFragment oaFragment = new OaFragment();
        return oaFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_oa;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        mLayoutManger = new CustomLayoutManger(mActivity);
        mLayoutManger.setSpeedFast();
        mOaRecyclerView.setLayoutManager(mLayoutManger);

        Drawable decLineDrawable = ContextCompat.getDrawable(mActivity, R.drawable.dev_line);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(
                mActivity, DividerItemDecoration.VERTICAL_LIST, decLineDrawable);
        itemDecoration.setHeight(1);
        mOaRecyclerView.addItemDecoration(itemDecoration);

        mOaAdapter = new OaAdapter(new ArrayList<OaInfoEntry>(), true);
        mOaAdapter.setLoadMoreView(new CustomLoadMoreView());
        mOaRecyclerView.setAdapter(mOaAdapter);

        RecyclerPaddingUtils.fitRecyclerViewPadding(mActivity, mOaRecyclerView);
        SwipeLayoutOffsetUtils.downSwipeLayoutOffset(mActivity, mSwipeRefreshLayout);

        mOaAdapter.setOnLoadMoreListener(this);
        mOaAdapter.setOnItemClickListener(new BaseQuickClickAdapter.OnItemClickListener<OaInfoEntry>() {
            @Override
            public void onItemClick(OaInfoEntry oaInfoEntry, int position) {
                mPresenter.markIsRead(oaInfoEntry, position);
                Intent intent = OaDetailActivity.newIntent(mActivity, oaInfoEntry);
                mActivity.startActivity(intent);
            }
        });

        mEmptyView = LayoutInflater.from(mActivity).inflate(R.layout.empty_view
                , (ViewGroup) mOaRecyclerView.getParent(), false);
        mErrorView = LayoutInflater.from(mActivity).inflate(R.layout.error_view
                , (ViewGroup) mOaRecyclerView.getParent(), false);
        TextView emptyTextView = (TextView) mEmptyView.findViewById(R.id.emptyTextView);
        emptyTextView.setText("查找不到搜索对应的结果");

        TextView errorTextView = (TextView) mErrorView.findViewById(R.id.errTextView);
        errorTextView.setText("加载失败，请点击重试");
        mErrorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });

        mSwipeRefreshLayout.setOnRefreshListener(this);

        StoreProxy.onRestoreInstanceState(savedInstanceState, mPresenter);
        mPresenter.subscribe();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        StoreProxy.onSaveInstanceState(outState, mPresenter);
    }

    @Override
    public void setPresenter(OaContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Override
    public void showDataEmpty() {
        mSwipeRefreshLayout.setEnabled(false);
        mOaAdapter.setEmptyView(mEmptyView);
    }

    @Override
    public void showDataError() {
        mOaAdapter.setEmptyView(mErrorView);
    }

    @Override
    public void showRefreshing(boolean isShow) {
        mSwipeRefreshLayout.setRefreshing(isShow);
    }

    @Override
    public void setOaInfoList(List<OaInfoEntry> oaInfoEntries) {
        mOaAdapter.setNewData(oaInfoEntries);
    }

    @Override
    public void appendOaInfoList(List<OaInfoEntry> oaInfoEntries) {
        mOaAdapter.addData(oaInfoEntries);
        mOaAdapter.loadMoreComplete();
    }

    @Override
    public void showLoadingFail() {
        mOaAdapter.loadMoreFail();
    }

    @Override
    public void showLoadingEnd() {
        mOaAdapter.loadMoreEnd();
    }

    @Override
    public void updateItem(int position) {
        mOaAdapter.notifyItemChanged(position);
    }

    @Override
    public void updateAllItem() {
        mOaAdapter.notifyDataSetChanged();
    }

    @Override
    public void updateAllNotRead() {
        mOaAdapter.clearRead();
        updateAllItem();
    }

    @Override
    public void moveToTop() {
        mOaRecyclerView.scrollToPosition(0);
        showAppBar();
    }

    @Override
    public void setRecyclerViewEnable(boolean enable) {
        mLayoutManger.setScrollEnabled(enable);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unsubscribe();
    }

    @Override
    public void onRefresh() {
        mPresenter.resetRefreshing();
        mPresenter.loadOaInfos();
    }

    @Override
    public void onLoadMoreRequested() {
        mPresenter.loadOaInfos();
    }

    @Override
    public void onToolbarDoubleClick() {
        mOaRecyclerView.smoothScrollToPosition(0);
    }

    @Override
    public void onClick() {
        mPresenter.clearAllRead();
    }
}
